/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.state.AutoshapeProperty;

public class AutoshapeMenuView extends RelativeLayout {
    private AutoshapeProperty.Color color;
    private AutoshapeProperty.Type type;
    private OnChangeListener listener;

    private View fill;

    private View[] types;
    private View[] colors;

    public AutoshapeMenuView(Context context) {
        super(context);
        init(context);
    }

    public AutoshapeMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AutoshapeMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        final Resources r = getResources();

        setBackgroundResource(R.drawable.ic_menu_bubble_autoshape);

//        final int paddingV = r.getDimensionPixelSize(R.dimen.autoshape_menu_padding_vertical);
//        final int paddingH = r.getDimensionPixelSize(R.dimen.autoshape_menu_padding_horizonal);
//        setPadding(paddingH, paddingV, paddingH, 0);

        inflate(context, R.layout.menu_autoshape, this);

        View circle = findViewById(R.id.circle);
        circle.setOnClickListener(typesOnClickListener);
        View oval = findViewById(R.id.oval);
        oval.setOnClickListener(typesOnClickListener);
        View square = findViewById(R.id.square);
        square.setOnClickListener(typesOnClickListener);
        View regular_triangle = findViewById(R.id.regular_triangle);
        regular_triangle.setOnClickListener(typesOnClickListener);
        View right_triangle = findViewById(R.id.right_triangle);
        right_triangle.setOnClickListener(typesOnClickListener);
        View star = findViewById(R.id.star);
        star.setOnClickListener(typesOnClickListener);
        View arrow = findViewById(R.id.arrow);
        arrow.setOnClickListener(typesOnClickListener);
        View line = findViewById(R.id.line);
        line.setOnClickListener(typesOnClickListener);
        types = new View[] {circle, oval, square, regular_triangle, right_triangle, star, arrow, line};

        View color1 = findViewById(R.id.color_1);
        color1.setOnClickListener(colorsOnClickListener);
        View color2 = findViewById(R.id.color_2);
        color2.setOnClickListener(colorsOnClickListener);
        View color3 = findViewById(R.id.color_3);
        color3.setOnClickListener(colorsOnClickListener);
        View color4 = findViewById(R.id.color_4);
        color4.setOnClickListener(colorsOnClickListener);
        View color5 = findViewById(R.id.color_5);
        color5.setOnClickListener(colorsOnClickListener);
        View color6 = findViewById(R.id.color_6);
        color6.setOnClickListener(colorsOnClickListener);
        View color7 = findViewById(R.id.color_7);
        color7.setOnClickListener(colorsOnClickListener);
        View color8 = findViewById(R.id.color_8);
        color8.setOnClickListener(colorsOnClickListener);
        View color9 = findViewById(R.id.color_9);
        color9.setOnClickListener(colorsOnClickListener);
        View color10 = findViewById(R.id.color_10);
        color10.setOnClickListener(colorsOnClickListener);
        View color11 = findViewById(R.id.color_11);
        color11.setOnClickListener(colorsOnClickListener);
        colors = new View[] {color1, color2, color3, color4, color5, color6, color7, color8, color9, color10, color11};

        fill = findViewById(R.id.fill);
        fill.setEnabled(false);
        fill.setOnClickListener(v -> {
            v.setSelected(!v.isSelected());

            if (listener != null) {
                listener.onSetAutoshapeFillEnabled(0, v.isSelected());
            }
        });
    }

    private final OnClickListener typesOnClickListener = v -> {
        for (View view : types) {
            view.setSelected(false);
        }
        v.setSelected(true);

        switch (v.getId()) {
            case R.id.circle:
                type = AutoshapeProperty.Type.CIRCLE;
                fill.setEnabled(true);
                break;
            case R.id.oval:
                type = AutoshapeProperty.Type.OVAL;
                fill.setEnabled(true);
                break;
            case R.id.square:
                type = AutoshapeProperty.Type.SQUARE;
                fill.setEnabled(true);
                break;
            case R.id.regular_triangle:
                type = AutoshapeProperty.Type.REGULAR_TRIANGLE;
                fill.setEnabled(true);
                break;
            case R.id.right_triangle:
                type = AutoshapeProperty.Type.RIGHT_TRIANGLE;
                fill.setEnabled(true);
                break;
            case R.id.star:
                type = AutoshapeProperty.Type.STAR;
                fill.setEnabled(true);
                break;
            case R.id.arrow:
                type = AutoshapeProperty.Type.ARROW;
                fill.setEnabled(false);
                break;
            case R.id.line:
                type = AutoshapeProperty.Type.LINE;
                fill.setEnabled(false);
                break;
        }

        if (listener != null) {
            listener.onSelectAutoshapeType(0, type);
        }
    };

    private final OnClickListener colorsOnClickListener = v -> {
        for (View view : colors) {
            view.setSelected(false);
        }
        v.setSelected(true);

        if (Props.getFlavorModel().equals(Props.Flavor.Model.Oem)) {
            switch (v.getId()) {
                case R.id.color_1:
                    color = AutoshapeProperty.Color.WHITE;
                    break;
                case R.id.color_2:
                    color = AutoshapeProperty.Color.BLACK;
                    break;
                case R.id.color_3:
                    color = AutoshapeProperty.Color.RED;
                    break;
                case R.id.color_4:
                    color = AutoshapeProperty.Color.GREEN;
                    break;
                case R.id.color_5:
                    color = AutoshapeProperty.Color.BLUE;
                    break;
                case R.id.color_6:
                    color = AutoshapeProperty.Color.YELLOW;
                    break;
                case R.id.color_7:
                    color = AutoshapeProperty.Color.BLUE_SOFT;
                    break;
                case R.id.color_8:
                    color = AutoshapeProperty.Color.GREEN_SOFT;
                    break;
                case R.id.color_9:
                    color = AutoshapeProperty.Color.PINK;
                    break;
                case R.id.color_10:
                    color = AutoshapeProperty.Color.VERMILION;
                    break;
                case R.id.color_11:
                    color = AutoshapeProperty.Color.YELLOW_SOFT;
                    break;
            }
        } else {
            switch (v.getId()) {
                case R.id.color_1:
                    color = AutoshapeProperty.Color.BLACK;
                    break;
                case R.id.color_2:
                    color = AutoshapeProperty.Color.WHITE;
                    break;
                case R.id.color_3:
                    color = AutoshapeProperty.Color.RED;
                    break;
                case R.id.color_4:
                    color = AutoshapeProperty.Color.YELLOW;
                    break;
                case R.id.color_5:
                    color = AutoshapeProperty.Color.GREEN;
                    break;
                case R.id.color_6:
                    color = AutoshapeProperty.Color.BLUE;
                    break;
//                case R.id.color_7:
//                    color = AutoshapeProperty.Color.PURPLE;
//                    break;
            }
        }

        if (listener != null) {
            listener.onSelectAutoshapeColor(0, color);
        }
    };

    public void setType(AutoshapeProperty.Type size) {
        if (size == null) {
            return;
        }

        this.type = size;

        int viewId = 0;
        switch (size) {
            case ARROW:
                viewId = R.id.arrow;
                break;
            case CIRCLE:
                viewId = R.id.circle;
                break;
            case LINE:
                viewId = R.id.line;
                break;
            case OVAL:
                viewId = R.id.oval;
                break;
            case REGULAR_TRIANGLE:
                viewId = R.id.regular_triangle;
                break;
            case RIGHT_TRIANGLE:
                viewId = R.id.right_triangle;
                break;
            case SQUARE:
                viewId = R.id.square;
                break;
            case STAR:
                viewId = R.id.star;
                break;
        }

        for (View view : types) {
            view.setSelected(view.getId() == viewId);
        }
    }

    public AutoshapeProperty.Type getType() {
        return type;
    }

    public void setColor(AutoshapeProperty.Color color) {
        if (color == null) {
            return;
        }

        this.color = color;

        int viewId = 0;
        if (Props.getFlavorModel().equals(Props.Flavor.Model.Oem)) {
            switch (color) {
                case WHITE:
                    viewId = R.id.color_1;
                    break;
                case BLACK:
                    viewId = R.id.color_2;
                    break;
                case RED:
                    viewId = R.id.color_3;
                    break;
                case BLUE:
                    viewId = R.id.color_5;
                    break;
                case GREEN:
                    viewId = R.id.color_4;
                    break;
                case YELLOW:
                    viewId = R.id.color_6;
                    break;
                case BLUE_SOFT:
                    viewId = R.id.color_7;
                    break;
                case GREEN_SOFT:
                    viewId = R.id.color_8;
                    break;
                case PINK:
                    viewId = R.id.color_9;
                    break;
                case VERMILION:
                    viewId = R.id.color_10;
                    break;
                case YELLOW_SOFT:
                    viewId = R.id.color_11;
                    break;
            }
        } else {
            switch (color) {
                case BLACK:
                    viewId = R.id.color_1;
                    break;
                case WHITE:
                    viewId = R.id.color_2;
                    break;
                case RED:
                    viewId = R.id.color_3;
                    break;
                case YELLOW:
                    viewId = R.id.color_4;
                    break;
                case GREEN:
                    viewId = R.id.color_5;
                    break;
                case BLUE:
                    viewId = R.id.color_6;
                    break;
//                case PURPLE:
//                    viewId = R.id.color_7;
//                    break;
            }
        }

        for (View view : colors) {
            view.setSelected(view.getId() == viewId);
        }
    }

    public AutoshapeProperty.Color getColor() {
        return color;
    }

    public void setFill(boolean fill) {
        this.fill.setSelected(fill);
    }

    public void setOnChangeListener(OnChangeListener listener) {
        this.listener = listener;
    }

    public interface OnChangeListener {
        void onSelectAutoshapeColor(int pointer, AutoshapeProperty.Color color);

        void onSelectAutoshapeType(int pointer, AutoshapeProperty.Type size);

        void onSetAutoshapeFillEnabled(int pointer, boolean fillEnabled);
    }
}
